package com.songaw.cangku.controller;

import com.songaw.cangku.base.util.UuidUtils;
import com.songaw.cangku.mapper.ClothesExMapper;
import com.songaw.cangku.mapper.ClothesMapper;
import com.songaw.cangku.mapper.ColorsMapper;
import com.songaw.cangku.pojo.PageInfo;
import com.songaw.cangku.pojo.Result;
import com.songaw.cangku.pojo.domain.AddClothesVo;
import com.songaw.cangku.pojo.domain.ClothesTableVo;
import com.songaw.cangku.pojo.domain.ClothesVo;
import com.songaw.cangku.pojo.domain.UpdateClothesVo;
import com.songaw.cangku.pojo.dto.ClothesDto;
import com.songaw.cangku.pojo.entity.Clothes;
import com.songaw.cangku.pojo.entity.ClothesTable;
import com.songaw.cangku.pojo.entity.Colors;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * TODO
 *
 * @author songaw
 * @date 2018/4/1 16:24
 */
@RestController
public class ClothesController {
    @Autowired
    ClothesMapper clothesMapper;
    @Autowired
    ClothesExMapper clothesExMapper;
    @Autowired
    ColorsMapper colorsMapper;
    @ApiOperation(value = "载片登记", notes = "载片登记")
    @RequestMapping(path="/v1/clothess",method = RequestMethod.POST)
    public Result<ClothesDto> createClothesAccount(@RequestBody AddClothesVo vo){
        try {
            String modelNumber = vo.getModelNumber();
            if(modelNumber ==null||"".equals(modelNumber)){
                return Result.getSystemErrorResult("款号不能为空");
            }
            String colorsStr = vo.getColors();
            if(colorsStr ==null||"".equals(colorsStr)){
                return Result.getSystemErrorResult("颜色不能为空");
            }
            String colorNumbersStr = vo.getColorNumber();
            if(colorNumbersStr ==null||"".equals(colorNumbersStr)){
                return Result.getSystemErrorResult("颜色对应数量不能为空");
            }
            String sizeStr = vo.getSize();
            if(sizeStr ==null||"".equals(sizeStr)){
                return Result.getSystemErrorResult("尺码不能为空");
            }
            String[] colors = colorsStr.split(",");
            String[] colorNumbers = colorNumbersStr.split(",");
            String[] sizes = sizeStr.split(",");
            if(colors.length!=colorNumbers.length){
                return Result.getSystemErrorResult("颜色跟数量不匹配");
            }
            try{
                for(int i=0;i<colorNumbers.length;i++){
                    Integer number = Integer.parseInt(colorNumbers[i]);
                    if (number==0){
                        return Result.getSystemErrorResult("颜色的数量不能为0");
                    }
                }
            }catch (Exception e){
                return Result.getSystemErrorResult("数量输入错误");
            }
            ClothesVo searchVo = new ClothesVo();
            searchVo.setModelNumber(modelNumber);
            List<ClothesDto> list = clothesExMapper.select(searchVo);
            if(list!=null&&list.size()>0){
                return Result.getSystemErrorResult("该款号已存在");
            }
            Clothes clothes = new Clothes();
            BeanUtils.copyProperties(vo,clothes);
            clothes.setUuid(UuidUtils.base58Uuid());
            clothes.setDeleteFlag("0");
            clothes.setInsertTime(new Date().getTime());
            clothes.setUpdateTime(new Date().getTime());

            List<Colors> colorsList = new ArrayList<>();
            try {
                for (int i = 0; i < colors.length; i++) {
                    for (int j = 0; j < sizes.length; j++) {
                        Colors c = new Colors();
                        c.setColorName(colors[i]);
                        Integer colorNumber = Integer.parseInt(colorNumbers[i]);
                        c.setColorNum(colorNumber);
                        c.setSizeName(sizes[j]);
                        c.setModelNumber(modelNumber);
                        c.setSizeSubmit(0);
                        c.setSizeClaimed(0);
                        c.setColorClaimed(0);
                        c.setColorSubmit(0);
                        Integer sizeNum=colorNumber / vo.getSizestyle();
                        if(sizeNum.intValue()*vo.getSizestyle().intValue()!=colorNumber.intValue()){
                            return Result.getSystemErrorResult("颜色数量跟尺码数量分配不均");
                        }
                        c.setSizeNum(sizeNum);
                        colorsList.add(c);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                return Result.getSystemErrorResult("颜色数量跟尺码数量分配不均"+e.getMessage());
            }
            clothesMapper.insert(clothes);
            ClothesDto clothesDto = new ClothesDto();
            BeanUtils.copyProperties(clothes,clothesDto);

            for(Colors color:colorsList){
                color.setUuid(UuidUtils.base58Uuid());
                colorsMapper.insert(color);
            }
            clothesDto.setColorList(colorsList);
            return Result.getSuccessResult(clothesDto);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }



    @RequestMapping(path="/v1/clothess/{uuid}",method = RequestMethod.GET)
    public Result<Clothes> findByLoginInfoUuid(@PathVariable("uuid") String uuid){
        try {
            Clothes clothes= clothesMapper.selectByPrimaryKey(uuid);
            if(clothes!=null) {
                return Result.getSuccessResult(clothes);
            }else{
                return Result.getSystemErrorResult("没有查到数据");
            }
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @ApiOperation(value = "载片登记修改", notes = "载片登记修改")
    @RequestMapping(path="/v1/clothess",method = RequestMethod.PUT)
    public Result<Integer> updateClothesAccount(@RequestBody UpdateClothesVo vo){
        try {
            // vo.setUuid(UuidUtils.base58Uuid());
            String modelNumber = vo.getModelNumber();
            String uuid = vo.getUuid();
            if(modelNumber ==null||modelNumber.equals("")){
                return Result.getSystemErrorResult("款号不能为空");
            }
            if(uuid ==null||uuid.equals("")){
                return Result.getSystemErrorResult("ID不能为空");
            }

            String colorsStr = vo.getColors();
            if(colorsStr ==null||"".equals(colorsStr)){
                return Result.getSystemErrorResult("颜色不能为空");
            }
            String colorNumbersStr = vo.getColorNumber();
            if(colorNumbersStr ==null||"".equals(colorNumbersStr)){
                return Result.getSystemErrorResult("颜色对应数量不能为空");
            }
            String sizeStr = vo.getSize();
            if(sizeStr ==null||"".equals(sizeStr)){
                return Result.getSystemErrorResult("尺码不能为空");
            }
            String[] colors = colorsStr.split(",");
            String[] colorNumbers = colorNumbersStr.split(",");
            String[] sizes = sizeStr.split(",");
            if(colors.length!=colorNumbers.length){
                return Result.getSystemErrorResult("颜色跟数量不匹配");
            }
            try{
                for(int i=0;i<colorNumbers.length;i++){
                    Integer number = Integer.parseInt(colorNumbers[i]);
                    if (number==0){
                        return Result.getSystemErrorResult("颜色的数量不能为0");
                    }
                }
            }catch (Exception e){
                return Result.getSystemErrorResult("数量输入错误");
            }
            Clothes clothes= clothesMapper.selectByPrimaryKey(uuid);



            List<Colors> colorsList = new ArrayList<>();
            try {
                for (int i = 0; i < colors.length; i++) {
                    Integer colorNumber = Integer.parseInt(colorNumbers[i]);

                    for (int j = 0; j < sizes.length; j++) {
                        Colors c = new Colors();
                        c.setColorName(colors[i]);
                        c.setColorNum(colorNumber);
                        c.setSizeName(sizes[j]);
                        c.setModelNumber(modelNumber);
                        c.setSizeSubmit(0);
                        c.setSizeClaimed(0);
                        c.setColorClaimed(0);
                        c.setColorSubmit(0);
                        Integer sizeNum=colorNumber / vo.getSizestyle();
                        if(sizeNum.intValue()*vo.getSizestyle().intValue()!=colorNumber.intValue()){
                            return Result.getSystemErrorResult("颜色数量跟尺码数量分配不均");
                        }
                        c.setSizeNum(sizeNum);
                        colorsList.add(c);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                return Result.getSystemErrorResult("颜色数量跟尺码数量分配不均"+e.getMessage());
            }

            ClothesDto clothesDto =null;
            ClothesVo searchVo = new ClothesVo();
            searchVo.setModelNumber(modelNumber);
            List<ClothesDto> list = clothesExMapper.select(searchVo);
            if(list!=null&&list.size()>0){
                if(list.size()>1) {
                    return Result.getSystemErrorResult("该款号已存在");
                }else{
                    clothesDto = list.get(0);
                    if(!uuid.equals(clothesDto.getUuid())){
                        return Result.getSystemErrorResult("该款号已存在");
                    }

                }
            }
            Map<String,Integer> map = new LinkedHashMap<>();
            List<Colors> resultColorsList=clothesDto.getColorList();
            for(Colors rc:resultColorsList){
                boolean result=false;
                for(Colors c:colorsList){
                    if(rc.getColorName().equals(c.getColorName())&&rc.getSizeName().equals(c.getSizeName())){
                        rc.setColorNum(rc.getColorNum()+c.getColorNum());
                        if(map.get(rc.getColorName())==null){
                            map.put(rc.getColorName(),rc.getColorNum());
                        }
                        rc.setSizeNum(rc.getSizeNum()+c.getSizeNum());
                        result=true;
                    }
                }
                if(!result){
                    return Result.getSystemErrorResult(rc.getColorName()+"跟原来的颜色不对应或者"+rc.getSizeName()+"跟原来的尺码不对应");
                }
                colorsMapper.updateByPrimaryKeySelective(rc);
            }
            String ns="";
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                ns+=value+",";
            }
            clothes.setColorNumber(ns);
            clothes.setUpdateTime(new Date().getTime());
            clothesMapper.updateByPrimaryKeySelective(clothes);
            return Result.getSuccessResult(1);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @ApiOperation(value = "载片登记删除", notes = "载片登记删除")
    @RequestMapping(path="/v1/clothess",method = RequestMethod.DELETE)
    public Result<Integer> deleteClothesAccount(@RequestParam("uuid") String uuid){
        try {
            Clothes c =clothesMapper.selectByPrimaryKey(uuid);
            if(c!=null){
                c.setDeleteFlag("1");
                clothesMapper.updateByPrimaryKey(c);
            }else{
                return Result.getSystemErrorResult("不存在");
            }

            return Result.getSuccessResult(1);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @RequestMapping(path = "/v1/clothess/list/search",method = RequestMethod.GET)
    public Result<PageInfo> findClothesAccountsPage(@ModelAttribute ClothesVo searchVo){
        PageInfo pageInfo = new PageInfo();
        List<ClothesDto>  list=  clothesExMapper.select(searchVo,searchVo.getRowBounds());
        int count = clothesExMapper.selectCount(searchVo);
        pageInfo.setTotal(count);
        pageInfo.setResults(list);
        pageInfo.setPageIndex(searchVo.getPageIndex());
        pageInfo.setPageSize(searchVo.getPageSize());
        return Result.getSuccessResult(pageInfo);
    }
    @ApiOperation(value = "根据款号统计", notes = "根据款号统计")
    @RequestMapping(path = "/v1/clothesTables/list/searchByModelNumber",method = RequestMethod.GET)
    public Result<List<Colors>>findClothesByModelNumber(@RequestParam("modelNumber") String modelNumber){

        List<Colors> list =  clothesExMapper.getColorsByModelNumber(modelNumber);

        return Result.getSuccessResult(list);
    }
    @RequestMapping(path = "/v1/clothess/list",method = RequestMethod.GET)
    public Result<List<ClothesDto>> findClothesAccounts(@ModelAttribute ClothesVo searchVo){

        List<ClothesDto>  list=  clothesExMapper.select(searchVo);

        return Result.getSuccessResult(list);
    }

}
